#!/usr/bin/python3
import os
from .Entity import Entity
from wrappers.OpenAPI_Wrapper import get_api_model_annotation, get_includes
from utils import My_Util2


class Entity_Base_VO:
    """
    实体视图模型基类
    """

    def __init__(self, context, entity: Entity) -> None:
        self.context = context
        self.entity: Entity = entity
        self.prefix: str = self.entity.table_name.split("_")[0].lower()
        self.my_util2 = My_Util2.My_Util(self.context)

    def code_includes(self):
        package = self.entity.package
        framework_package = f"""{self.context['package']}.{self.context['framework_package']}"""
        i18n = False
        currency = False
        decimal = False
        date = False
        local_date_time = False
        for field in self.entity.fields:
            if field.ignore():
                continue
            if field.i18n:
                i18n = True
            if field.currency:
                currency = True
            if field.java_type == "BigDecimal":
                decimal = True
            if field.java_type == "Date":
                date = True
            if field.java_type == "LocalDateTime":
                local_date_time = True
        includes = list()
        includes.append(f"""import {framework_package}.support.model.BaseVO;""")
        if currency:
            includes.append(f"""import {package}.internal.currency.Currency;""")
        if i18n:
            includes.append(f"""import {package}.internal.i18n.NameInfo;""")
        includes += get_includes("model")
        includes += get_includes("field")
        includes.append(f"""import lombok.Data;""")
        includes.append(f"""import lombok.EqualsAndHashCode;""")
        includes.append(f"""import lombok.ToString;""")
        includes.append(f"""import lombok.experimental.Accessors;""")
        if decimal:
            includes.append("import java.math.BigDecimal;")
        if date:
            includes.append("import java.util.Date;")
        if local_date_time:
            includes.append("import java.time.LocalDateTime;")
        includes = self.my_util2.sort_import_order(includes)
        return "\r".join(includes)

    def code_fields(self):
        field_code = list()
        for field in self.entity.fields:
            if field.column_name != "id" and field.ignore():
                continue
            field_code.append(field.code(openapi=True, verify=False))
        return field_code

    def save_file(self, dir):
        _target_dir = os.path.join(dir, self.context["code_storage"]["root"], "vo")
        if not os.path.exists(_target_dir):
            os.makedirs(_target_dir)
        with open(
            os.path.join(_target_dir, "{}BaseVO.java".format(self.entity.name)),
            mode="w",
            encoding="utf-8",
        ) as f:
            f.write(self.code())

    def code(self):
        package = self.entity.package
        entity_name = self.entity.name
        comment = self.entity.comment
        author = self.entity.author

        includes = self.code_includes()
        code_fields = self.code_fields()
        if len(code_fields) > 0:
            code_fields = "\r".join(code_fields)

        return f"""package {package}.{self.context["code_storage"]["root"]}.vo;

{includes}

/**
 * <p>
 * {comment}视图对象
 * </p>
 *
 * @author {author}
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@ToString(callSuper = true)
{get_api_model_annotation(f"""{entity_name}BaseVO""", comment)}
public class {entity_name}BaseVO extends BaseVO {"{"}
{code_fields if len(code_fields) > 0 else ""}
{"}"}
"""
